﻿using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Interactivity;
using System.ComponentModel;

namespace OpenLightGroup.Common.Behaviors
{
    /// <summary>
    /// Allows a user to press the delete or backspace key to uncheck a toggle button
    /// </summary>
     [Description("Allows a user to press the delete or backspace key to uncheck a toggle button")]
    public class RemoveToggleButtonCheckBehavior : Behavior<ToggleButton>
    {

         /// <summary>
         /// Wires up a handler for the KeyUp event
         /// </summary>
        protected override void OnAttached()
        {
            base.OnAttached();

            this.AssociatedObject.KeyUp += new KeyEventHandler(AssociatedObject_KeyUp);

        }

        /// <summary>
        /// Removes the event handler from the KeyUp event.
        /// </summary>
        protected override void OnDetaching()
        {
            base.OnDetaching();

            this.AssociatedObject.KeyUp -= AssociatedObject_KeyUp;

        }

        private void AssociatedObject_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Delete || e.Key == Key.Back)
                this.AssociatedObject.IsChecked = false;

        }
    }
}
